/* *****************************************************************************************
 
Made by www.Flashphoner.com, all rights reserved.
These sources can only be used for working with Flashphoner server.
If you want use this to create your own application (separate from Flashphoner server) � 
please contact support@flashphoner.com 

******************************************************************************************/

package com.flashphoner.api.management
{
	import flash.media.Microphone;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundCodec;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;
	
	public class SoundControl
	{
		
		[Embed(source="/sounds/CALL_OUT.mp3")]
		private static var ringClass:Class;		
		[Embed(source="/sounds/BUSY.mp3")]
		private static var busyClass:Class;
		[Embed(source="/sounds/REGISTER.mp3")]
		private static var registerClass:Class;
		[Embed(source="/sounds/HANGUP.mp3")]
		private static var finishClass:Class;

		private var ringSound:Sound;		
		private var busySound:Sound;
		private var registerSound:Sound;
		private var finishSound:Sound;
		private var ringSoundChannel:SoundChannel;

				
		public static var RING_SOUND:String = null;
		public static var BUSY_SOUND:String = null;
		public static var REGISTER_SOUND:String = null;
		public static var FINISH_SOUND:String = null;
				
		private static var soundControl:SoundControl;
		
		private var mic:Microphone;
		
		public function SoundControl()
		{
			mic = Microphone.getEnhancedMicrophone();		
			mic.codec = SoundCodec.SPEEX;
			mic.framesPerPacket = 1;
			mic.rate = 16;
			mic.gain = 50;
			mic.encodeQuality = 6;
			mic.soundTransform = new SoundTransform(1,0);			
			mic.setLoopBack(false);			
			mic.setSilenceLevel(0,3600000);
			mic.setUseEchoSuppression(true);
		}
		
		
		public static function getInstance():SoundControl{
			if (soundControl == null){
				soundControl = new SoundControl();
				soundControl.ringSound = Sound(new ringClass());			
				soundControl.busySound = Sound(new busyClass());
				soundControl.registerSound = Sound(new registerClass());
				soundControl.finishSound = Sound(new finishClass());	    
			}
			return soundControl;
		}
	    
	    public function updateSounds():void{
	    	if (SoundControl.RING_SOUND != null){
				ringSound = new Sound(new URLRequest(SoundControl.RING_SOUND));
	    	}
	    	if (SoundControl.BUSY_SOUND != null){
				busySound = new Sound(new URLRequest(SoundControl.BUSY_SOUND));
	    	}
	    	if (SoundControl.REGISTER_SOUND != null){
				registerSound = new Sound(new URLRequest(SoundControl.REGISTER_SOUND));
	    	}
	    	if (SoundControl.FINISH_SOUND != null){
				finishSound = new Sound(new URLRequest(SoundControl.FINISH_SOUND));
	    	}
	    }

		public function playBusySound():void{
			busySound.play(0,1);			
		}
		
		public function playFinishSound():void{
			finishSound.play(0,1);		
		}
		
		public function playRegisterSound():void{
			registerSound.play(0,1);		
		}			
		
		public function stopRingSound():void{
			if (ringSoundChannel!=null){
				ringSoundChannel.stop();
				ringSoundChannel = null;
			}
		}
		public function playRingSound():void{
			if (ringSoundChannel == null){
				ringSoundChannel = ringSound.play(0,999);
			}			
		}
		
		public function changeMicrophone(microphone:Microphone,isLoopback:Boolean,gain:Number = -1):void{
			var oldGain:int = this.mic.gain;
			this.mic = microphone;
			if (this.mic != null){
				mic.codec = SoundCodec.SPEEX;
				mic.framesPerPacket = 1;
				mic.rate = 16;
				if (gain != -1){
					mic.gain = gain;
				}else{
					mic.gain = oldGain;
				}
				mic.encodeQuality = 6;
				mic.soundTransform = new SoundTransform(1,0);			
				mic.setLoopBack(isLoopback);			
				mic.setSilenceLevel(0,3600000);
				mic.setUseEchoSuppression(true);
			}
		}
		
		public function getMicrophone():Microphone{
			return mic;
		}		
		

	}
}